var public = {
    // 字符串加密
    encryption:function(str){
        // 前6个和后4个显示，其余**
        str = str.replace(/^(\w{6})(\w*)(\w{4})$/, function (all,u1,u2,u3){
          return u1 + new Array(u2.length+1).join('*') + u3;
        });
        return str;
    },
    // 禁止在非微信浏览器中打开
    IsItWeChat:function(){
        var useragent = navigator.userAgent;
        if (useragent.match(/MicroMessenger/i) != 'MicroMessenger') {
            alert('已禁止本次访问：您必须使用微信内置浏览器访问本页面！');
            var opened = window.open('about:blank', '_self');
            opened.opener = null;
        } 
    },
    documentNomove:function(){
        $(document).on('touchmove',function(event){
            event.stopPropagation();
            return false;
        });
    },
    // 安卓input弹框弹起来偏移
    AndroidOffset:function(){
        document.documentElement.style.height = window.innerHeight + 'px';
    },
    // 得到url参数
    getUrl:function(getMseagee){
        var reg = new RegExp("(^|&)" + getMseagee + "=([^&]*)(&|$)", "i");
        var r = decodeURI(window.location.search).substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
    },
    // 解决微信返回不刷新的问题
    WeChatRefresh:function(){
        var isPageHide = false; 
        window.addEventListener('pageshow', function () { 
            if (isPageHide) { 
              location.reload(true);
            }
        });
        window.addEventListener('pagehide', function () {
            isPageHide = true;
        });
    },
    // 禁止横屏
    RotateScreen:function(){
        (function rotate(){
           var orientation=window.orientation;
           if(orientation==90||orientation==-90){
              document.body.style.display='none';
              alert("请使用竖屏访问！");
           };
           window.onorientationchange=function(){
              document.body.style.display="block";
              rotate();
           };
        })();
    },
    addCSSAnimation:function(ele,anType,anendfn){
        $(ele).addClass("animated "+anType).on("webkitAnimationEnd",function(){
            $(ele).removeClass("animated "+anType);
            $(ele).off("webkitAnimationEnd");
            if(!anendfn){
                return;
            }else{
                $(anendfn).hide();
            }
        });
    },
    // 判断空对象
    EmptyObject:function(obj){
        for(var key in obj) {
            return false;
        };
        return true;
    },
    // 屏蔽微信分享功能
    ShareWeChat:function(){
        var bRotate = false;
        function onBridgeReady() {
            WeixinJSBridge.call('hideOptionMenu');
        };
        if (typeof WeixinJSBridge == "undefined") {
            if (document.addEventListener) {
                document.addEventListener('WeixinJSBridgeReady', onBridgeReady,
                        false);
            } else if (document.attachEvent) {
                document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
            }
        } else {
            onBridgeReady();
        }
    },
    // 判断活动下线时间
    Downtime:function(nowTime,endTime,url){
        if(!url){
            url = "./ActiveDownline.html";
        };
        // 后台给的时间或者客户端现有的时间
        var nowData = new Date(nowTime);
        // 要下线的时间注意时间格式要对如下格式
        var endtime = new Date(endTime);
        if(endtime < nowData){
            var opened = window.open(url, '_self');
            opened.opener = null;
        };
    },
    // 重写alert，去掉url地址
    RewriteAlert:function(){
        window.alert = function(name){
            var iframe = document.createElement("IFRAME");
            iframe.style.display="none";
            iframe.setAttribute("src", 'data:text/plain,');
            document.documentElement.appendChild(iframe);
            window.frames[0].window.alert(name);
            iframe.parentNode.removeChild(iframe);
        };
    },
    // 自定义提示弹框
    CustomAlert:function(text,longtime){
        if(!longtime){
            longtime = 3000;
        };
        var alerthtml = ('<div class="alertfixed"><span class="animated bounceInUp spanalert">'+text+'</span></div>');
        $('body').prepend(alerthtml);
        var timer = setTimeout(function(){
            $('.alertfixed').remove();
            clearTimeout(timer);
        },longtime);
    },
    // loading效果需要传入一个loading圆圈的颜色默认有一个颜色
    FnLoading:function(color){
        var loadinghtml = ('<div class="loading"><div class="spinner"><div class="bounce1" style="background:'+color+';"></div><div class="bounce2" style="background:'+color+';"></div><div class="bounce3" style="background:'+color+';"></div></div></div>');
        $('body').prepend(loadinghtml);
    },
    FnIponeAlert:function(){
         //高仿iphone弹出层
        (function ($) {
            //ios confirm box
            jQuery.fn.confirm = function (title,option,okCall, cancelCall){
                var defaults = {
                    title: null, //提示语
                    cancelText: '取消', //修改取消文字
                    okText: '确定' //修改确定文字
                };
                if (undefined === option) {
                    option = {};
                }
                if ('function' != typeof okCall) {
                    okCall = $.noop;
                }
                if ('function' != typeof cancelCall) {
                    cancelCall = $.noop;
                }
                var o = $.extend(defaults, option, {title: title, okCall: okCall, cancelCall: cancelCall});
                var $dom = $(this);
                var dom = $('<div class="g-plugin-confirm">');
                var dom1 = $('<div>').appendTo(dom);
                var dom_content = $('<div>').html(o.title).appendTo(dom1);
                var dom_btn = $('<div>').appendTo(dom1);
                var btn_cancel = $('<a href="#"></a>').html(o.cancelText).appendTo(dom_btn);
                var btn_ok = $('<a href="#"></a>').html(o.okText).appendTo(dom_btn);
                btn_cancel.on('click', function (e) {
                    o.cancelCall();
                    dom.remove();
                    e.preventDefault();
                });
                btn_ok.on('click', function (e) {
                    o.okCall();
                    dom.remove();
                    e.preventDefault();
                });
                dom.appendTo($('body'));
                return $dom;
            };
            //使用说明
            //$(document).confirm('提示文字',{cancelText:"取消了",okText:"确定了"},function(){确定的},function(){取消的});
            //使用说明结束
        })(jQuery);
    },
    // 向上滑动的提示  
    moUpTo:function(ele,text){
        var moUpTohtml = ('<span class="motoup"><i></i><br/><em>'+text+'</em></span>');
        $(ele).prepend(moUpTohtml);
    },
    // 移动端拖拽并且控制元素大小
    Drag:function(){
        $(document).on("touchmove",function(e) {
            e.stopPropagation();
            e.preventDefault();
            if (!!this.move) {
                var posix = !document.move_target ? {'x': 0, 'y': 0} : document.move_target.posix,
                    callback = document.call_down || function() {
                        var _touch = e.originalEvent.targetTouches[0];
                        if(_touch.pageX<120 || _touch.pageX>$("body").width()-5){
                            return false;
                        }else{
                            $(this.move_target).css({
                                'top': _touch.pageY - posix.y,
                                'left': _touch.pageX - posix.x
                            });
                        } 
                    };
                callback.call(this, e, posix);
            }
        }).on("touchend",function(e) {
            e.stopPropagation();
            e.preventDefault();
            if (!!this.move) {
                var callback = document.call_up || function(){};
                callback.call(this, e);
                $.extend(this, {
                    'move': false,
                    'move_target': null,
                    'call_down': false,
                    'call_up': false
                });
            }
        });
        // 封装使用方法
        function changeDrage(target,drag){
            // 多保留几位保证每个都不一样
            var $ele = Math.random().toFixed(20);
                $ele = $(target).on("touchstart",function(e) {
                e.stopPropagation();
                e.preventDefault();
                $(this).css({"z-index":"100"}).siblings().css({"z-index":"1"});
                var _touch = e.originalEvent.targetTouches[0];
                var offset = $(this).offset();
                this.posix = {'x': _touch.pageX - offset.left, 'y': _touch.pageY - offset.top};
                $.extend(document, {'move': true, 'move_target': this});
            }).on('touchstart', drag, function(e) {
                var _this = $(this);
                e.stopPropagation();
                e.preventDefault();
                var _touch = e.originalEvent.targetTouches[0];
                var posix = {
                        'w': _this.parent().width(), 
                        'h': _this.parent().height(), 
                        'x': _touch.pageX, 
                        'y': _touch.pageY
                    };
                
                $.extend(document, {'move': true, 'call_down': function(e) {
                    e.stopPropagation();
                    e.preventDefault();
                    var _touch = e.originalEvent.targetTouches[0];
                    _this.parent().css({
                        'width': Math.max(30, _touch.pageX - posix.x + posix.w),
                        'height': Math.max(30, _touch.pageY - posix.y + posix.h)
                    });
                }});
                return false;
            });
        };
        // 使用方法示例
        // changeDrage("#box传入要拖拽的对象","#coor传入控制大小的对象");
    },
    publicInit:function(){
        this.FnIponeAlert();
        this.FnLoading();
        this.RewriteAlert();
        this.RotateScreen();
        this.WeChatRefresh();
    }
}